package com.ny.zmb.openapi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ny.zmb.openapi.constant.PHPConstant;
import com.ny.zmb.openapi.controller.entity.Result;
import com.ny.zmb.openapi.entity.PmOrderSplitAccountLog;
import com.ny.zmb.openapi.mapper.PmOrderSplitAccountLogMapper;
import com.ny.zmb.openapi.service.PmOrderSplitAccountLogService;
import com.ny.zmb.openapi.ysutil.YsHttpUtil;
import com.ny.zmb.openapi.ysutil.common.ServiceEnum;
import com.ny.zmb.openapi.ysutil.from.BizReqJsonCollege.BizReqJsonSeparateAccounts;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.ny.zmb.openapi.constant.PHPConstant.*;

/**
 * 分账订单记录
 *
 * @author ChenZhiZheng
 * @date 20241107
 */
@Service
public class PmOrderSplitAccountLogServiceImpl extends ServiceImpl<PmOrderSplitAccountLogMapper, PmOrderSplitAccountLog> implements PmOrderSplitAccountLogService {
    @Resource
    private YsHttpUtil ysHttpUtil;
    @Resource
    private PmOrderSplitAccountLogMapper pmOrderSplitAccountLogMapper;

    private QueryWrapper<PmOrderSplitAccountLog> queryWrapper = new QueryWrapper<>();

    @Override
    public PmOrderSplitAccountLog getByOrigRequestNo(String origRequestNo) {
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orig_request_no", origRequestNo);
        return pmOrderSplitAccountLogMapper.selectOne(queryWrapper);
    }

    @Override
    public PmOrderSplitAccountLog getByRequestNo(String requestNo) {
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("request_no", requestNo);
        return pmOrderSplitAccountLogMapper.selectOne(queryWrapper);
    }

    /**
     * 分账总API
     *
     * @param obj           对象
     * @param srcMerchantNo 商户号
     * @param methodStr
     * @return Result
     */
    @Override
    public Result SeparateAccounts(BizReqJsonSeparateAccounts obj, String srcMerchantNo, String methodStr) {
        JSONObject result;
        switch (methodStr.trim()) {
            // 分账登记
            case "divisionRegister":
                // 回调处理
                if (obj != null && (obj.getNotifyUrl() == null || obj.getNotifyUrl().isEmpty())) {
                    obj.setNotifyUrl(BASE_URL + "/api/index/divisionNotify");
                }
                // 获取记录
                PmOrderSplitAccountLog pmOrderSplitAccountLog = new PmOrderSplitAccountLog(obj);
                // 初保存
                save(pmOrderSplitAccountLog);
                result = doActive(ServiceEnum.DIVISION_REGISTER_V2, methodStr, srcMerchantNo, obj);
                // 更新
                updateById(pmOrderSplitAccountLog.update(result.getString("state"), result.getString("subMsg")));

                break;
            // 分账查询
            case "divisionQuery":
                result = doActive(ServiceEnum.DIVISION_QUERY, methodStr, srcMerchantNo, obj);
                break;
            // 分账资金回退
            case "divisionBack":
                result = doActive(ServiceEnum.DIVISION_BACK, methodStr, srcMerchantNo, obj);
                // 查询成功
                pmOrderSplitAccountLog = getByOrigRequestNo(obj.getOrigRequestNo());
                if (pmOrderSplitAccountLog == null) {
                    // 没有找到记录,不更新
                    break;
                } else {
                    // 更新
                    updateById(pmOrderSplitAccountLog.update(result.getString("state"), result.getString("subMsg")));
                }
                break;
            // 分账资金回退授权
            case "divisionBackAuth":
                result = doActive(ServiceEnum.DIVISION_BACKAUTH, methodStr, srcMerchantNo, obj);
                break;
            case "terminalDivisionRegister":
                result = doActive(ServiceEnum.TERMINALDIVISIONREGISTER, methodStr, srcMerchantNo, obj);
                break;
            // 终端分账结果查询
            case "terminalDivisionQuery":
                result = doActive(ServiceEnum.TERMINALDIVISIONQUERY, methodStr, srcMerchantNo, obj);
                break;
            // 查询余额
            case "queryBalance":
                result = doActive(ServiceEnum.QUERY_AMOUNT, methodStr, srcMerchantNo, obj);
                break;
            default:
                throw new RuntimeException("地址错误");
        }
        try {
            return Result.ok(result);
        } catch (Exception var) {
            return Result.fail(var.getMessage());
        }

    }

    /**
     * 分账登记
     *
     * @param obj           对象
     * @param srcMerchantNo 商户号
     * @return Result
     */
    @Override
    public Result divisionRegister(BizReqJsonSeparateAccounts obj, String srcMerchantNo) {
        try {
            // 回调处理
            if (obj != null && (obj.getNotifyUrl() == null || obj.getNotifyUrl().isEmpty())) {
                obj.setNotifyUrl(BASE_URL + "/api/index/divisionNotify");
            }
            // 获取记录
            PmOrderSplitAccountLog pmOrderSplitAccountLog = new PmOrderSplitAccountLog(obj);
            // 初保存
            save(pmOrderSplitAccountLog);
            JSONObject result = doActive(ServiceEnum.DIVISION_REGISTER_V2, ServiceEnum.DIVISION_REGISTER_V2.getApiName(), srcMerchantNo, obj);
            // 更新
            updateById(pmOrderSplitAccountLog.update(result.getString("state"), result.getString("subMsg")));
            return Result.ok(result);
        } catch (Exception var) {
            return Result.fail(var.getMessage());
        }
    }

    /**
     * 分账登记查询
     *
     * @param obj           对象
     * @param srcMerchantNo 商户号
     * @return Result
     */
    @Override
    public Result divisionQuery(BizReqJsonSeparateAccounts obj, String srcMerchantNo) {
        try {
            JSONObject result = doActive(ServiceEnum.DIVISION_QUERY, ServiceEnum.DIVISION_QUERY.getApiName(), srcMerchantNo, obj);
            return Result.ok(result);
        } catch (Exception var) {
            return Result.fail(var.getMessage());
        }
    }

    /**
     * 分账资金回退
     *
     * @param obj           对象
     * @param srcMerchantNo 商户号
     * @return Result Result
     */
    @Override
    public Result divisionBack(BizReqJsonSeparateAccounts obj, String srcMerchantNo) {
        try {
            JSONObject result = doActive(ServiceEnum.DIVISION_BACK, ServiceEnum.DIVISION_BACK.getApiName(), srcMerchantNo, obj);
            // 查询成功
            PmOrderSplitAccountLog pmOrderSplitAccountLog = getByOrigRequestNo(obj.getOrigRequestNo());
            if (pmOrderSplitAccountLog == null) {
                // 没有找到记录,不更新
            } else {
                // 更新
                updateById(pmOrderSplitAccountLog.update(result.getString("state"), result.getString("subMsg")));
            }
            return Result.ok(result);
        } catch (Exception var) {
            return Result.fail(var.getMessage());
        }
    }

    /**
     * 分账资金回退授权
     *
     * @param obj           对象
     * @param srcMerchantNo 商户号
     * @return Result
     */
    @Override
    public Result divisionBackAuth(BizReqJsonSeparateAccounts obj, String srcMerchantNo) {
        try {
            JSONObject result = doActive(ServiceEnum.DIVISION_BACKAUTH, ServiceEnum.DIVISION_BACKAUTH.getApiName(), srcMerchantNo, obj);
            return Result.ok(result);
        } catch (Exception var) {
            return Result.fail(var.getMessage());
        }
    }

    /**
     * 终端分账登记
     *
     * @param obj           对象
     * @param srcMerchantNo 商户号
     * @return Result
     */
    @Override
    public Result terminalDivisionRegister(BizReqJsonSeparateAccounts obj, String srcMerchantNo) {
        try {
            JSONObject result = doActive(ServiceEnum.TERMINALDIVISIONREGISTER, ServiceEnum.TERMINALDIVISIONREGISTER.getApiName(), srcMerchantNo, obj);
            return Result.ok(result);
        } catch (Exception var) {
            return Result.fail(var.getMessage());
        }
    }

    /**
     * 终端分账结果查询
     *
     * @param obj           对象
     * @param srcMerchantNo 商户号
     * @return Result
     */
    @Override
    public Result terminalDivisionQuery(BizReqJsonSeparateAccounts obj, String srcMerchantNo) {
        try {
            JSONObject result = doActive(ServiceEnum.TERMINALDIVISIONQUERY, ServiceEnum.TERMINALDIVISIONQUERY.getApiName(), srcMerchantNo, obj);
            return Result.ok(result);
        } catch (Exception var) {
            return Result.fail(var.getMessage());
        }
    }


    private JSONObject doActive(ServiceEnum anyEnum, String apiName, String srcMerchantNo, Object obj) {
        try {
            JSONObject result = ysHttpUtil.sendBase(anyEnum, "prd", null, null, apiName, srcMerchantNo, obj);
            return result;
        } catch (Exception var) {
            throw new RuntimeException(var.getMessage());
        }
    }

}
